package com.wise.service.cms.content.successarticlecolumn;

import cn.hutool.core.collection.CollectionUtil;
import com.wise.entities.content.Article;
import com.wise.entities.content.ArticleColumn;
import com.wise.entities.content.SuccessArticle;
import com.wise.entities.content.SuccessArticleColumn;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.framework.daomodel.DaoPageList;
import com.wise.framework.restful.PageResultList;
import com.wise.summer.v1.cms.content.columns.ListColumns;
import com.wise.summer.v1.cms.content.successcolumns.ListSuccessColumns;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@SummerService
public class ListSuccessColumnsService implements SummerServiceBean<ListSuccessColumns> {
    @Override
    public void sum(ListSuccessColumns model) {
        SqlSelect sql = SqlSelect.selec("*")
                .fromClassName(SuccessArticleColumn.class)
                .like(!StringUtils.isEmpty(model.getColumnName()), SuccessArticleColumn._columnName, "%" + model.getColumnName() + "%")
                .eq(!Objects.isNull(model.getShowState()), SuccessArticleColumn._showState, model.getShowState())
                .eqLang(SuccessArticleColumn._lang)
                .order_by(SuccessArticleColumn._sort)
                .order_by(SuccessArticleColumn._id + " desc");

        PageResultList<SuccessArticleColumn> articleColumnPageResultList = DaoPageList.s(SuccessArticleColumn.class, sql, model.getStart(), model.getLimit());
        //写入文章数量
        List<Object> columnId = articleColumnPageResultList
                .getList()
                .stream()
                .map(SuccessArticleColumn::getId)
                .collect(Collectors.toList());

        List<SuccessArticle> articles = findArticle(columnId);

        Map<String, Integer> articleCountGroupbyArticleColumn = articles.parallelStream().collect(Collectors.groupingBy(SuccessArticle::getColumnId, Collectors.reducing(0, e -> 1, Integer::sum)));
        if (CollectionUtil.isNotEmpty(articleCountGroupbyArticleColumn)) {
            articleColumnPageResultList.getList().forEach(it ->
                    it.setNum(articleCountGroupbyArticleColumn.get(it.getId()))
            );
        }
        model.setSummerResult(articleColumnPageResultList);
    }

    /**
     * 查询文章数据
     */
    private List<SuccessArticle> findArticle(List<Object> list) {
        if (CollectionUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        SqlSelect sql = SqlSelect.selec("*");
        sql.fromClassName(SuccessArticle.class);
        sql.in(SuccessArticle._columnId, list);
        return DaoList.s(SuccessArticle.class, sql);
    }
}
